/*
 depotlocatorTests.m
 Date Created: February 22, 2010
 
 Author: Karol Krizka <kkrizka@gmail.com>
 
 Copyright (C) 2010 Green-Power
 
 URL: http://code.google.com/p/green-power
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 $Id$
 */

#import "depotlocatorTests.h"

#import "DLStaticGoogleLocalQuery.h"
#import "DLRecyclingDepot.h"

@implementation depotlocatorTests

#if USE_APPLICATION_UNIT_TEST     // all code under test is in the iPhone Application


#else                           // all code under test must be linked into the Unit Test bundle

- (void)testGoogleQueryParsing {
  validGoogleQueryParsing=[[DLStaticGoogleLocalQuery alloc] initWithQuery:@""];
  validGoogleQueryParsing.delegate=self;
  
  // Tests are run relative to the project root directory
  [validGoogleQueryParsing queryFile:@"Tests/googlelocalsearch-sampleresults.txt"];

  invalidGoogleQueryParsing=[[DLStaticGoogleLocalQuery alloc] initWithQuery:@""];
  invalidGoogleQueryParsing.delegate=self;
  [invalidGoogleQueryParsing queryFile:@"Tests/googlelocalsearch-samplebadresults.txt"];
}

#endif

- (void)googleLocalQuery:(DLGoogleLocalQuery* )query finishedWithStatus:(NSUInteger)status results:(NSArray *)depots {
  if(query==validGoogleQueryParsing) {
    CLLocationCoordinate2D coordinate;
    
    DLRecyclingDepot *depot0 = [[DLRecyclingDepot alloc] init];
    depot0.name = @"Kensington Return It Center";
    depot0.address = @"6518 Hastings Street, Burnaby";
    coordinate.latitude=49.280338;
    coordinate.longitude=-122.969376;
    depot0.coordinate = coordinate;
    DLRecyclingDepot *depot1 = [[DLRecyclingDepot alloc] init];
    depot1.name = @"Regional Recycling";
    depot1.address = @"12a, 2350 Beta Ave, Burnaby";
    coordinate.latitude=49.262497;
    coordinate.longitude=-122.993179;
    depot1.coordinate = coordinate;
    DLRecyclingDepot *depot2 = [[DLRecyclingDepot alloc] init];
    depot2.name = @"Metrotown Bottle Return It Centre";
    depot2.address = @"4760 Imperial Street, Burnaby";
    coordinate.latitude=49.221931;
    coordinate.longitude=-122.996841;
    depot2.coordinate = coordinate;
    DLRecyclingDepot *depot3 = [[DLRecyclingDepot alloc] init];
    depot3.name = @"Powell Street Return-It Bottle Depot";
    depot3.address = @"1856 Powell Street, Vancouver";
    coordinate.latitude=49.284492;
    coordinate.longitude=-123.066681;
    depot3.coordinate = coordinate;
    DLRecyclingDepot *depot4 = [[DLRecyclingDepot alloc] init];
    depot4.name = @"Kitchener Bottle Depot";
    depot4.address = @"4177 Halifax Street, Burnaby";
    coordinate.latitude=49.268074;
    coordinate.longitude=-123.011772;
    depot4.coordinate = coordinate;
    DLRecyclingDepot *depot5 = [[DLRecyclingDepot alloc] init];
    depot5.name = @"Regional Recycling";
    depot5.address = @"960 Evans Avenue, Vancouver";
    coordinate.latitude=49.270626;
    coordinate.longitude=-123.081772;
    depot5.coordinate = coordinate;
    
    NSInteger count=[depots count];
    STAssertEquals(6,count,@"There should be 6 depots");
    
    DLRecyclingDepot *depot=[depots objectAtIndex:0];
    STAssertEqualObjects(depot0.name,depot.name,@"Names should be equal");
    STAssertEqualObjects(depot0.address,depot.address,@"Addresses should be equal");
    STAssertEqualsWithAccuracy(depot0.coordinate.latitude,depot.coordinate.latitude,0.000001,@"Latitudes should be equal within double's accuracy");          
    STAssertEqualsWithAccuracy(depot0.coordinate.longitude,depot.coordinate.longitude,0.000001,@"Longitudes should be equal within double's accuracy");

    depot=[depots objectAtIndex:1];
    STAssertEqualObjects(depot1.name,depot.name,@"Names should be equal");
    STAssertEqualObjects(depot1.address,depot.address,@"Addresses should be equal");
    STAssertEqualsWithAccuracy(depot1.coordinate.latitude,depot.coordinate.latitude,0.000001,@"Latitudes should be equal within double's accuracy");          
    STAssertEqualsWithAccuracy(depot1.coordinate.longitude,depot.coordinate.longitude,0.000001,@"Longitudes should be equal within double's accuracy");

    depot=[depots objectAtIndex:2];
    STAssertEqualObjects(depot2.name,depot.name,@"Names should be equal");
    STAssertEqualObjects(depot2.address,depot.address,@"Addresses should be equal");
    STAssertEqualsWithAccuracy(depot2.coordinate.latitude,depot.coordinate.latitude,0.000001,@"Latitudes should be equal within double's accuracy");          
    STAssertEqualsWithAccuracy(depot2.coordinate.longitude,depot.coordinate.longitude,0.000001,@"Longitudes should be equal within double's accuracy");
    
    depot=[depots objectAtIndex:3];
    STAssertEqualObjects(depot3.name,depot.name,@"Names should be equal");
    STAssertEqualObjects(depot3.address,depot.address,@"Addresses should be equal");
    STAssertEqualsWithAccuracy(depot3.coordinate.latitude,depot.coordinate.latitude,0.000001,@"Latitudes should be equal within double's accuracy");          
    STAssertEqualsWithAccuracy(depot3.coordinate.longitude,depot.coordinate.longitude,0.000001,@"Longitudes should be equal within double's accuracy");
    
    depot=[depots objectAtIndex:4];
    STAssertEqualObjects(depot4.name,depot.name,@"Names should be equal");
    STAssertEqualObjects(depot4.address,depot.address,@"Addresses should be equal");
    STAssertEqualsWithAccuracy(depot4.coordinate.latitude,depot.coordinate.latitude,0.000001,@"Latitudes should be equal within double's accuracy");          
    STAssertEqualsWithAccuracy(depot4.coordinate.longitude,depot.coordinate.longitude,0.000001,@"Longitudes should be equal within double's accuracy");
    
    depot=[depots objectAtIndex:5];
    STAssertEqualObjects(depot5.name,depot.name,@"Names should be equal");
    STAssertEqualObjects(depot5.address,depot.address,@"Addresses should be equal");
    STAssertEqualsWithAccuracy(depot5.coordinate.latitude,depot.coordinate.latitude,0.000001,@"Latitudes should be equal within double's accuracy");          
    STAssertEqualsWithAccuracy(depot5.coordinate.longitude,depot.coordinate.longitude,0.000001,@"Longitudes should be equal within double's accuracy");
  }
  else {
    STFail(@"Unknown query returned results");
  }

}

- (void)googleLocalQuery:(DLGoogleLocalQuery* )query finishedWithParseError:(NSError *)error {
  if(query != invalidGoogleQueryParsing)
    STFail(@"A query that should have been valid failed");
}

@end
